package com.bigdata.mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class WordCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {

    private IntWritable comVal = new IntWritable();
    /*
    map阶段的reduce操作，优化MR程序的shuffle网络IO传输
    key：map输出的key
    values：是一个mapTask输出的相同key的value数据集合
    把key和values经过逻辑处理，写出到reduce Task中处理
     */

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        // word count
        Iterator<IntWritable> iter = values.iterator();
        int sum = 0;
        while (iter.hasNext()) {
            IntWritable num = iter.next();
            sum += num.get();
        }
        comVal.set(sum);
        System.out.println("combiner reduce-----key: " + key.toString() + ",value:" + comVal.get());
        context.write(key, comVal);

    }
}
